# $Id$
# Maintainer: Sébastien "Seblu" Luttringer <seblu@archlinux.org>

pkgname=ceph
pkgver=10.2.9
pkgrel=1
pkgdesc='Distributed, fault-tolerant storage platform delivering object, block, and file system'
arch=('x86_64' 'i686')
url='https://ceph.com/'
license=('GPL')
makedepends=('boost' 'systemd' 'xfsprogs' 'python2-sphinx' 'cython2' 'sed')
depends=('boost-libs' 'curl' 'expat' 'fcgi' 'fuse2' 'gcc-libs' 'glibc'
  'gperftools' 'keyutils' 'leveldb' 'libaio' 'libatomic_ops' 'libedit'
  'libsystemd' 'libutil-linux' 'ncurses' 'nss' 'python2' 'snappy'
  'python2-setuptools' 'xfsprogs')
options=('emptydirs')
source=("https://download.ceph.com/tarballs/$pkgname-$pkgver.tar.gz"
        'ceph.sysusers'
        '01-ceph-detec-init.patch')
md5sums=('eff6db61b3befb686e0693befa1f076a'
         'b3e24e3aa005a657ab475f84bfe3291a'
         'da25c78ae413dc134cc99fe9818f6ff1')

prepare() {
  cd $pkgname-$pkgver
  # apply patch from the source array (should be a pacman feature)
  local filename
  for filename in "${source[@]}"; do
    if [[ "$filename" =~ \.patch$ ]]; then
      msg2 "Applying patch ${filename##*/}"
      patch -p1 -N -i "$srcdir/${filename##*/}"
    fi
  done
  :
}

build() {
  cd $pkgname-$pkgver
  # fix xfs.h failure in configure on i686
  [[ $CARCH == i686 ]] && export CXXFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64"
  # don't use autotools for ceph-disk, ceph-detect and tests
  sed -ri 's,include (ceph-disk|ceph-detect-init|test)/Makefile.am,,' src/Makefile.am
  # regen configure
  ./autogen.sh
  # fix python-config binary name
  sed -i 's,python-config,python2-config,g' configure
  ./configure \
    --prefix=/usr \
    --sbindir=/usr/bin \
    --libexecdir=/usr/lib \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --with-man-pages \
    --without-radosgw \
    --without-openldap \
    PYTHON=/usr/bin/python2 \
    CYTHON_CHECK=yes \
    PYTHON_CONFIG_CHECK=yes \
    SPHINX_BUILD=sphinx-build2
  make
}

package() {
  cd $pkgname-$pkgver

  # main install
  make DESTDIR="$pkgdir" install

  # ceph-disk
  pushd src/ceph-disk
  python2 setup.py install --root "$pkgdir" --prefix=/usr
  popd

  # ceph-detect-init
  pushd src/ceph-detect-init
  python2 setup.py install --root "$pkgdir" --prefix=/usr
  popd

  # systemd stuff
  install -Dm644 systemd/ceph.tmpfiles.d "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"
  install -Dm644 "$srcdir"/ceph.sysusers "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"

  # fix bash completions path
  msg2 'Fix bash completion path'
  install -d -m 755 "$pkgdir"/usr/share/bash-completion
  mv "$pkgdir"/{etc/bash_completion.d,usr/share/bash-completion/completions}

  # fix python2 shebang, did not do it in prepare() anymore because it
  # confuse automake
  msg2 'Fix python2 shebang'
  find "$pkgdir" -type f -executable -exec \
    sed -i '1s,^#! \?/usr/bin/\(env \|\)python$,#!/usr/bin/python2,' {} \;
}

# vim:set ts=2 sw=2 et:
